<?php
namespace Mouta\AmistateBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
use Mouta\AmistateBundle\utils\Strings;

class TagsAdmin extends Admin{
    
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('id', null, array('required' => false,'read_only' => true))
            ->add('tag', null, array('required' => true))
            ->add('tag_google', null, array('required' => false))
            ->add('id_form', null, array('required' => true))
            ->add('validado', 'checkbox', array('required' => false))
        ;
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('id')
            ->add('tag')
            ->add('tag_google')
            ->add('id_form')
            ->add('validado')
        ;
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('tag')
            ->add('tag_google')
            ->add('id_form')
            ->add('validado')
        ;
    }

    public function validate(ErrorElement $errorElement, $object)
    {
        /*$errorElement
            ->with('tag')
                ->assertMaxLength(array('limit' => 32))
            ->end()
        ;*/
    }
    
    public function preUpdate($tag)
    {
        $tag->setTagGoogle( Strings::getSeoUrl($tag->getTag()) );
        parent::preUpdate($tag);
    }
    
}

?>
